package model;

import javax.servlet.http.HttpServletRequest;

public class College extends BaseModel{

	private Integer id;
	private String cName;
	
  	private Integer pageNo;//页码，第几页
  	private Integer rowCount;//单页行数
	
	public College() {
		
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getcName() {
		return cName;
	}

	public void setcName(String cName) {
		this.cName = cName;
	}
	
	
	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getRowCount() {
		return rowCount;
	}

	public void setRowCount(Integer rowCount) {
		this.rowCount = rowCount;
	}

	//封装工作单元，将请求中的参数赋值给对应的变量
	public void getParams(HttpServletRequest req) {
		this.id         = getIValueINM(req.getParameter("id"));
		this.cName      = getValueINM(req.getParameter("cName"));

		this.pageNo     = dealPageNo(req.getParameter("pageNo"));
		this.rowCount   = dealRowCount(req.getParameter("rowCount"));
	}

	//组装查询总行数sql
	public String getTotalCountSql() {
		StringBuffer sql = new StringBuffer("select count(*) from college where 1=1 ");
		if (id != null) {
			sql.append(" and id = ").append(id);
		}
		if (cName != null) {
			sql.append(" and c_name = '").append(cName).append("'");
		}
		return sql.toString();
	}
	//组装插入sql语句
	public String getInsertSql() {
		//拼接values
		StringBuffer values = new StringBuffer();
		
		//拼接columns
		StringBuffer sql = new StringBuffer("insert into college(");
		
		if (id != null) {
			sql.append("id,");
			values.append(id + ",");
		}
		if (cName != null) {
			sql.append("c_name,");
			values.append("'").append(cName).append("',");
		}
		//删除字段拼接的最后一个逗号
		sql.deleteCharAt(sql.length()-1);
		//删除值拼接的最后一个逗号
		values.deleteCharAt(values.length()-1);
		
		sql.append(")values(").append(values).append(")");
		
		return sql.toString();
	}

	public String getSelectSql() {
		StringBuffer sql = new StringBuffer("select * from college where 1=1");
		
		if (id != null) {
			sql.append(" and id = ").append(id);
		}
		if (cName != null) {
			sql.append(" and c_name = '").append(cName).append("'");
		}
		return sql.toString();
	}

	public String getUpdateSql() {
		//拼接values
		StringBuffer values = new StringBuffer();
		//拼接columns
		StringBuffer sql = new StringBuffer("update college ");
		
		if (id != null) {
			values.append(" id = ").append(id).append(",");
		}
		if (cName != null) {
			values.append(" c_name = '").append(cName).append("',");
		}
		if (values.length() > 0 ) {
			//删除值拼接的最后一个逗号
			values.deleteCharAt(values.length()-1);
			
			sql.append(" set ").append(values).append(" where id = ").append(id);
			return sql.toString();
		}else {
			return null;
		}
	}
}
